package com.kunbo.cn.service.impl.uflo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import com.kunbo.cn.entity.EasyUiCombobox;
import com.kunbo.cn.entity.EasyUiDataGrid;
import com.kunbo.cn.util.db.DbQuerySql;


//@Service
public class EasyuiServiceImpl {
	
	@Autowired
    private JdbcTemplate jdbcTemplate;

	private void debug(String s)
	{
		//System.out.println(s);
	}

	private Logger loger = LoggerFactory.getLogger(EasyuiServiceImpl.class);
	
	/*********************************EasyUi前台组件接口***********************************/
	///////////////////////////////////////////////////// DataGrid ///////////////////////////////////////////////////
	public EasyUiDataGrid getEasyUiDataGrid(String table,String page,String row) throws SQLException
	{
		EasyUiDataGrid ret=null;
		
		String sql = "";						
		sql+=" SELECT * FROM "+table; 
		sql+=" ORDER BY "+table+".id DESC";	
		sql+=DbQuerySql.limit(page, row);
		
		String count_sql = "";	
		count_sql+=" SELECT * FROM "+table;
		
		ret = getEasyUiDataGrid(sql, count_sql);			
		return ret;
	}
	
	public EasyUiDataGrid getEasyUiDataGrid(String sql,String count_sql) 
	{
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		List<Map<String, Object>> list_len = jdbcTemplate.queryForList(count_sql);
		//System.out.println(sql);
		//System.out.println(count_sql);
		return new EasyUiDataGrid(list_len.size(),list);		
	}

	public  EasyUiCombobox getEasyUiCombobox(String sql) throws SQLException
	{
		loger.info("sql = "+sql);
		EasyUiCombobox e = new EasyUiCombobox();		
		ArrayList<Map<String, Object>> ret= (ArrayList<Map<String, Object>>) getEasyUiDataGridNoPage(sql);    
		for(Map<String, Object> m:ret)
		{
			e.add(m);
		}	
		return e; 
	}
	
	//首页ERP表数据
//	public  EasyUiCombobox getEasyUiComboboxForERP(String sql) throws SQLException
//	{
//		EasyUiCombobox e = new EasyUiCombobox();		
//		ArrayList<Map<String, Object>> ret= (ArrayList<Map<String, Object>>) getEasyUiForERP(sql);    
//		for(Map<String, Object> m:ret)
//		{
//			e.add(m);
//		}	
//
//		return e; 
//	}
	public List<Map<String,Object>> getEasyUiDataGridNoPage(String sql)
	{
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	
	//首页ERP表数据
//	public List<Map<String,Object>> getEasyUiForERP(String sql)
//	{
//		List<Map<String,Object>> ls=null;
//		ErpSqlUtil util = new ErpSqlUtil();
//		Connection con = util.getConnection();
//		Statement stmt;
//		try {
//			stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//			//System.out.println("sql1="+sql);
//			con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//	        ResultSet rs = stmt.executeQuery(sql);
//	        
//	        ls = Util.ResultSetToList(rs); 
//		} catch (SQLException e) {
//			e.printStackTrace();
//			return null;
//		}
//		
//		return ls;
//	}
}
